package com.zlkj.pro.modular.app.service.impl;

import cn.stylefeng.roses.kernel.file.modular.service.SysFileInfoService;
import cn.stylefeng.roses.kernel.file.modular.service.impl.SysFileInfoServiceImpl;
import com.anji.captcha.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlkj.pro.core.util.ConvertUtil;
import com.zlkj.pro.modular.app.service.AppProductService;
import com.zlkj.pro.modular.business.entity.ProductInfoEntity;
import com.zlkj.pro.modular.business.entity.ProductTypeEntity;
import com.zlkj.pro.modular.business.mapper.ProductInfoMapper;
import com.zlkj.pro.modular.business.mapper.ProductTypeMapper;
import com.zlkj.pro.modular.business.model.dto.ProductInfoDTO;
import com.zlkj.pro.modular.business.model.vo.ProductInfoVO;
import com.zlkj.pro.modular.business.model.vo.ProductTypeVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * @author liyang
 * @date 2024/4/6
 * @time 14:34
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class AppProductServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfoEntity> implements AppProductService {

    @Autowired
    private ProductTypeMapper productTypeMapper;

    @Autowired
    private Environment env;
    @Autowired
    private ProductInfoMapper productInfoMapper;
    @Autowired
    private SysFileInfoService sysFileInfoService;


    @Override
    public List<ProductTypeVO> findList(ProductInfoDTO dto) {

        // 查询分类列表
        LambdaQueryWrapper<ProductTypeEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ProductTypeEntity::getDelFlag, "N");
        queryWrapper.orderByAsc(ProductTypeEntity::getDataSort);
        List<ProductTypeEntity> productTypelist = productTypeMapper.selectList(queryWrapper);
        List<ProductTypeVO> typeList = ConvertUtil.turn(productTypelist, ProductTypeVO.class);

        // 查询产品列表
        List<ProductInfoVO> infoList = productInfoMapper.findList(dto);
        if (CollectionUtils.isNotEmpty(infoList)) {
            infoList.forEach(vo -> {
                if (vo.getImageDetailFileId() != null) {
                    vo.setImageDetailFileUrl(env.getProperty("file-path") + vo.getImageDetailFileId());
                }
                if (vo.getManualFileId() != null) {
                    vo.setManualFileUrl(env.getProperty("file-path") + vo.getManualFileId());
                }
            });
        }

        typeList.forEach(type -> {
            List<ProductInfoVO> thisInfos = infoList.stream().filter(info -> Objects.equals(info.getProductTypeId(), type.getId())).collect(Collectors.toList());
            type.setProductInfoList(thisInfos);
        });
        return typeList;
    }


    @Override
    public ProductInfoVO findDetail(Long id) {
        ProductInfoEntity info = super.getById(id);
        ProductInfoVO vo = ConvertUtil.turn(info, ProductInfoVO.class);
        if (vo.getImageDetailFileId() != null) {
            vo.setImageDetailFileUrl(env.getProperty("file-path") + vo.getImageDetailFileId());
            vo.setImageDetailFileName(sysFileInfoService.getById(vo.getImageDetailFileId()).getFileOriginName());
        }

        if (vo.getManualFileId() != null) {
            vo.setManualFileUrl(env.getProperty("file-path") + vo.getManualFileId());
            vo.setManualFileName(sysFileInfoService.getById(vo.getManualFileId()).getFileOriginName());
        }
        return vo;
    }

}
